package com.adopt.service.Impl;


import com.adopt.mapper.AdminMapper;
import com.adopt.pojo.Admin;
import com.adopt.pojo.Result;
import com.adopt.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * ClassName:AdminServiceImpl
 * Package:com.adopt.service.Impl
 * Description:
 *
 * @Date:2023/9/8 16:56
 * Author:shifan
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    public Result getAdminByAdminName(String adminName) {
        if ("" == adminName || StringUtils.isEmpty(adminName)){
            return new Result(100,"管理员名为空");
        }
        Admin admin = adminMapper.getAdminByAdminName(adminName);
        return new Result(200,"成功获取",admin);
    }



    public Result addAdmin(Admin admin) {
        int row = adminMapper.insert(admin);
        if (row != 1) {

            return new Result(100, "异常情况");
        }
        return new Result(200, "添加成功");
    }

    @Override
    public List<Admin> getAdmins() {

        List<Admin> admins = adminMapper.getAdmins();

        return admins;

    }

/*
    @Override
    public int deleteAdmin(Integer id) {
        int row = adminMapper.deleteAdmin(id);
        return row;
    }
*/
@Override

    public Result updateAdmin(Admin admin) {
        if(StringUtils.isEmpty(admin.getAdminName())){
            return new Result(100,"用户名不能为空");
        }
        if(StringUtils.isEmpty(admin.getAdminPwd())){
            return new Result(100,"密码不能为空");
        }

        int row=adminMapper.updateAdmin(admin);

        if(row!=1){
            return new Result(200,"修改失败");
        }
        return new Result(100,"修改成功");
    }



    @Override
    public Admin findById(Integer id) {
        Admin admin = adminMapper.findById(id);
        //查询得到的管理员列表
        return admin;
    }


    @Override
    public List<Admin> findByName(String adminName) {
        List<Admin> byName = adminMapper.findByName(adminName);
        return byName;
    }

    @Override
    public void deleteBatch(List<Integer> ids) {

        adminMapper.deleteBatch(ids);
    }

    @Override
    public Result deleteAdmin(Integer id) {
        int row=adminMapper.deleteAdmin(id);
        if(row!=1){

            return new Result(200,"异常情况");

        }
        return new Result(100,"删除成功");
    }


}
